#include "../inc/Environment.hxx"
#include "../inc/Antenna.hxx"
#include "../inc/Utils.hxx"
#include <iostream>

using namespace std;

Environment::Environment() {}

void Environment::addTarget(Target target) {
    this->targets.push_back(target);
}

void Environment::getResponse(Pulse* pulse, Echo* echo) {
    vector<Target>::iterator targetIterator;
    echo->setElevationAngle(0);
    for(targetIterator=targets.begin(); targetIterator != targets.end(); ++targetIterator) {
        Target target = *targetIterator;
        double minTargetAzimuth = target.getCoordinates().getAzimuth() - target.getAngleWidth()/2;
        double maxTargetAzimuth = target.getCoordinates().getAzimuth() + target.getAngleWidth()/2;
        if(Antenna::AZIMUTH >= minTargetAzimuth && Antenna::AZIMUTH <= maxTargetAzimuth) { // sprawdzam czy zgadzaja sie azymuty anteny i ceil
            cout << ".:TARGET DETECTED:."; 
            cout << "\tTarget min_az = " << minTargetAzimuth;
            cout << "\tSignal az = " << Antenna::AZIMUTH;
            cout << "\tTarget max_az = " << maxTargetAzimuth;
            cout << "\tTarget dist = " << target.getCoordinates().getDistance() << endl;
            echo->setElevationAngle(target.getCoordinates().getElevation());
            double targetDistance = target.getCoordinates().getDistance();
            int distance2gateNumber = targetDistance/Utils::RANGE_BIN_LENGTH;
            //wpisywanie impulsu w tablice echa
            vector<complex<double> >::iterator echoIterator = echo->begin() + (distance2gateNumber-pulse->size()/2);
            for(int i = 0; i < pulse->size(); i++) {
                (*echoIterator) += pulse->at(i);
                echoIterator++;
            }
        }
    }
//    return echo;
}

void Environment::updateEnvironment() {
    cout << "Environment update:" << endl;
    vector<Target>::iterator targetIterator;
    for(targetIterator = targets.begin(); targetIterator != targets.end(); ++targetIterator) {
        (*targetIterator).updateCoordinates();
    }
}

